#include "VESFlow.h"
#include "VMain.h"
#include "esflow/QuadState.h"
#include "esflow/QuadInterpolator.h"

VESFlow::VESFlow(void)
{
    _baseobj = new baiscobj;
}


VESFlow::~VESFlow(void)
{
    SAFE_RELEASE(_baseobj);
}

void VESFlow::init()
{
    loadTextures();
    _baseobj->init2();
    _id = 0;
}

void VESFlow::setEnvironmentBeforeRender() const
{
    glEnable(GL_DEPTH_TEST);

    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluPerspective(40.0f,
                   (GLfloat)_w / (GLfloat)_h,
                   0.1f, 1000.0f);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    glViewport(_x, VIEWPORT_HEIGHT - _y - _h, _w, _h);

    _baseobj->DisplayScene();
}

void VESFlow::setEnvironmentAfterRender() const
{
    glDisable(GL_DEPTH_TEST);
    VMain::resetViewport();
    VMain::resetGLEnviroment();
}

bool VESFlow::render() const
{
    if (!VRRWidget::render())
    {
        return false;
    }

    setEnvironmentBeforeRender();
    _baseobj->Update();
    _baseobj->play();
    glPushMatrix();
    glTranslatef(-0.5f, 0, 0);
    _baseobj->m_pState->Render();
    glPopMatrix();
    setEnvironmentAfterRender();

    return true;

}

bool VESFlow::loadTextures()
{
    ArrayTextures::iterator iter = _baseobj->m_cactus.begin();
    int picinc = 0;
    bool ret = true;

    for (; iter != _baseobj->m_cactus.end(); ++iter)
    {
        iTexture& itex = *iter;

        ret = VMain::loadTexture(
                  VMain::getSkin("main_cover\\%d.png", picinc += 1),
                  &itex.ID, &itex.width, &itex.height, NULL)
              && ret;
    }

    return ret;
}

void VESFlow::scrollLeft()
{
    DEBUG_OUTPUT("Scroll Left!");
    _baseobj->SetDirection(1);
}

void VESFlow::scrollRight()
{
    DEBUG_OUTPUT("Scroll Right!");
    _baseobj->SetDirection(0);
}

void VESFlow::mouseDown(int x, int y)
{
    UNUSED(y);
    _oldx = x;
    _pressdown = true;
    _id = _baseobj->click(x, y);
}

void VESFlow::mouseUp(int x, int y)
{
    if (_pressdown)
    {
        if (_oldx == -1)
        {
            return;
        }

        if (x < _oldx)
        {
            scrollLeft();
        }
        else if (x > _oldx)
        {
            scrollRight();
        }
    }

    switch (_id)
    {
    case 0:
        //        MessageBox(0,L"1",L"",MB_OK);
        VMain::switchToUi(UI_CATALOGRY);
        break;
    case 1:
        //        MessageBox(0,L"2",L"",MB_OK);
        VMain::switchToUi(UI_LYRICS);
        break;
    case 2:
        //        MessageBox(0,L"3",L"",MB_OK);
        VMain::switchToUi(UI_SINGER);

        break;
    case 3:
        //        MessageBox(0,L"4",L"",MB_OK);
        VMain::switchToUi(UI_POP);
        break;
    case 4:
        //        MessageBox(0,L"5",L"",MB_OK);
        VMain::switchToUi(UI_RANK);
        break;
    case 5:
        //        MessageBox(0,L"6",L"",MB_OK);
        VMain::switchToUi(UI_PINYIN);
        break;
    case 6:
        //        MessageBox(0,L"7",L"",MB_OK);
        VMain::switchToUi(UI_LANGUAGE);
        break;
    default:
        break;
    }

    _pressdown = false;
    _oldx = -1;
}

void VESFlow::move(int x, int y)
{
    VWidget::move(x, y);
}

void VESFlow::resize(int w, int h)
{
    VWidget::resize(w, h);
}
